using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using visAd.ExceptionHandler;

namespace visAd.ServerDataAddIn
{
    public enum FilterType
    {
        ServerType=0,
        ServerNames =1,
        Fields = 2,
        FieldAttributes =3        
    }

    public partial class FilterXMLWizard : Form
    {
        XmlDocument serverList;
        private DataTable dtServerTypes;
        private DataTable dtServerNames;
        private DataTable dtFieldNames;
        private DataTable dtFieldAttributes;
        
        private FilterType currentFilterType;
        private string currentServerType;
        private static int currentType=1;
        private static Hashtable controlWizardFlow;
       
        private visAdHelper visadHelper;
        private Microsoft.Office.Interop.Visio.Application vsoApplication;
        private bool IsUsePersistedFilter = false;
        private const string mClassName = "FilterXMLWizard";
        
        public FilterXMLWizard(XmlDocument FilterXML, visAdHelper visAd, Microsoft.Office.Interop.Visio.Application vapp, bool IsUseExistingFilter)
        {
            InitializeComponent();
            if (FilterXML == null)
            {
                this.Close();
                return;
            }
            serverList = FilterXML;
            visadHelper = visAd;
            this.vsoApplication = vapp;
            IsUsePersistedFilter = IsUseExistingFilter;
        }

        private void FilterXMLWizard_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsUsePersistedFilter)
                {
                    RePopulateFilterData();
                }
                else
                {
                    PopulateXMLData();
                    visAdHelper.ServerListChanged = false;
                }
                CheckNextButtonStatus();
                btnBack.Enabled = false;

                btnFinish.Enabled = false;
                btnFinish.Visible = false;

                clbFilterXML.Focus();
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "FilterXMLWizard_Load");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                //throw new vaCustomException(ex, mClassName, "FilterXMLWizard_Load");
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
        }

        private void PopulateXMLData()
        {
            try
            {

                XmlNodeList nodeList;
                bool IsAddNewColumn = true;

                //populate server types
                nodeList = serverList.SelectNodes("/visAd/va-servertype");
                dtServerTypes = new DataTable("va-servertype");
                dtServerNames = new DataTable("va-servers");
                dtFieldNames = new DataTable("va-field");
                dtFieldAttributes = new DataTable("va-filterattribute");                

                dtServerTypes = GetDataTable(nodeList, dtServerTypes, "", IsAddNewColumn);

                for (int ServerTypeCount = 0; ServerTypeCount < dtServerTypes.Rows.Count; ServerTypeCount++)
                {
                    string ServerType = dtServerTypes.Rows[ServerTypeCount][0].ToString();
                    //if (serverList == null)
                    //{
                    //    this.Close();
                    //    return;
                    //}
                    //populate all the server ids
                    nodeList = serverList.SelectNodes("/visAd/va-servertype[@type='" + ServerType + "']/va-servers");
                    dtServerNames = GetDataTable(nodeList, dtServerNames, ServerType, IsAddNewColumn);

                    nodeList = serverList.SelectNodes("/visAd/va-servertype[@type='" + ServerType + "']/va-filter/va-field");
                    dtFieldNames = GetDataTable(nodeList, dtFieldNames, ServerType, IsAddNewColumn);

                    for (int rowCount = 0; rowCount < dtFieldNames.Rows.Count; rowCount++)
                    {
                        string FieldName = dtFieldNames.Rows[rowCount][0].ToString();

                        if (ServerType == dtFieldNames.Rows[rowCount]["ServerType"].ToString())
                        {
                            nodeList = serverList.SelectNodes("/visAd/va-servertype[@type='" + ServerType + "']/va-filter/va-field[@fieldname='" + FieldName + "']/va-filterattribute");
                            dtFieldAttributes = GetDataTable(nodeList, dtFieldAttributes, ServerType, IsAddNewColumn);
                            dtFieldAttributes = UpdateFieldAttributes(dtFieldAttributes, FieldName, ServerType, IsAddNewColumn);
                            //foreach(XmlNode node in nodeList)
                            //{
                            //    if (node.Attributes.GetNamedItem("supprotdistinct").Value == "true")
                            //    {
                            //        dtSelectCriteria = GetSelectCriteria(dtSelectCriteria, node.Attributes.GetNamedItem("filtername").Value, ServerType);
                            //    }
                            //}
                        }
                        IsAddNewColumn = false;
                    }

                    IsAddNewColumn = false;
                }

                //load the Server Type details
                DisplayFilterServerType();
                currentType = 1;

            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "PopulateXMLData");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
       
        private DataTable UpdateFieldAttributes(DataTable dtFieldAttributes, string fieldName, string serverType, bool IsAddColumn)
        {
            try
            {
                if (IsAddColumn)
                {
                    dtFieldAttributes.Columns.Add("Field", System.Type.GetType("System.String"));                    
                }

                for (int rowCount = 0; rowCount < dtFieldAttributes.Rows.Count; rowCount++)
                {
                    if (dtFieldAttributes.Rows[rowCount]["Field"].ToString() == string.Empty)
                    {
                        dtFieldAttributes.Rows[rowCount]["Field"] = fieldName;
                    }
                    dtFieldAttributes.Rows[rowCount]["IsSelected"] = true;
                }
                
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "UpdateFieldAttributes");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return (dtFieldAttributes);
        }

        private DataTable GetDataTable(XmlNodeList nodelist, DataTable dtData, string serverType,bool isAddNewColumn)
        {
            try
            {
                XmlNode node = null;

                if (nodelist == null)
                    return null;

                int nodeCount = nodelist.Count;

                // get parameter names
                node = nodelist.Item(0);
                if (node == null)
                    return null;

                XmlAttributeCollection attrCollection = node.Attributes;
                if (attrCollection == null)
                    return null;
                if (attrCollection.Count == 0)
                    return null;

                if (isAddNewColumn)
                {
                    foreach (XmlAttribute attrName in attrCollection)
                    {
                        dtData.Columns.Add(attrName.Name);
                    }
                    dtData.Columns.Add("IsSelected", System.Type.GetType("System.Boolean"));
                    dtData.Columns.Add("ServerType", System.Type.GetType("System.String"));
                }

                // add rows
                DataRow drAttr = null;
                foreach (XmlNode n in nodelist)
                {
                    drAttr = dtData.NewRow();
                    foreach (XmlAttribute attrVal in n.Attributes)
                    {
                        drAttr[attrVal.Name] = attrVal.Value;
                        drAttr["IsSelected"] = false;
                        drAttr["ServerType"] = serverType;
                    }
                    dtData.Rows.Add(drAttr);
                }

                dtData.AcceptChanges();
                
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "GetDataTable");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return dtData;
        }

        private void RePopulateFilterData()
        {

            try
            {
                XmlNodeList nodeList;
                bool IsAddNewColumn = true;

                serverList = visadHelper.GetPersistedFilter();

                //serverList = Utilities.GetServerDefaultXMLFilter();

                //populate server types
                nodeList = serverList.SelectNodes("/visAd/va-servertype");
                dtServerTypes = new DataTable("va-servertype");
                dtServerNames = new DataTable("va-servers");
                dtFieldNames = new DataTable("va-field");
                dtFieldAttributes = new DataTable("va-filterattribute");

                dtServerTypes = GetDataTable(nodeList, dtServerTypes, IsAddNewColumn);

                for (int ServerTypeCount = 0; ServerTypeCount < dtServerTypes.Rows.Count; ServerTypeCount++)
                {
                    string ServerType = dtServerTypes.Rows[ServerTypeCount][0].ToString();

                    //populate all the server ids
                    if (visAdHelper.ServerListChanged)
                    {
                        XmlDocument newServerList = Utilities.GetServerDefaultXMLFilter();
                        if (newServerList == null)
                        {
                            this.Close();
                            return;
                        }
                        nodeList = newServerList.SelectNodes("/visAd/va-servertype[@type='" + ServerType + "']/va-servers");
                        dtServerNames = GetDataTable(nodeList, dtServerNames, ServerType, IsAddNewColumn);
                    }
                    else
                    {
                        //if (serverList == null)
                        //{
                        //    this.Close();
                        //    return;
                        //}
                        nodeList = serverList.SelectNodes("/visAd/va-servertype[@type='" + ServerType + "']/va-servers");
                        dtServerNames = GetDataTable(nodeList, dtServerNames, IsAddNewColumn);
                    }

                    nodeList = serverList.SelectNodes("/visAd/va-servertype[@type='" + ServerType + "']/va-filter/va-field");
                    dtFieldNames = GetDataTable(nodeList, dtFieldNames, IsAddNewColumn);

                    for (int rowCount = 0; rowCount < dtFieldNames.Rows.Count; rowCount++)
                    {
                        string FieldName = dtFieldNames.Rows[rowCount][0].ToString();

                        if (ServerType == dtFieldNames.Rows[rowCount]["ServerType"].ToString())
                        {
                            nodeList = serverList.SelectNodes("/visAd/va-servertype[@type='" + ServerType + "']/va-filter/va-field[@fieldname='" + FieldName + "']/va-filterattribute");
                            dtFieldAttributes = GetDataTable(nodeList, dtFieldAttributes, ServerType, IsAddNewColumn);
                            dtFieldAttributes = UpdateFieldAttributes(dtFieldAttributes, FieldName, ServerType, IsAddNewColumn);
                        }
                        IsAddNewColumn = false;
                    }

                    IsAddNewColumn = false;
                }
                //visAdHelper.ServerListChanged = false;
                DisplayFilterServerType();
                setNavigationControl();
                currentType = 1;
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "RePopulateFilterData");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private DataTable GetDataTable(XmlNodeList nodelist, DataTable dtData, bool isAddNewColumn)
        {
            try
            {
                XmlNode node = null;

                if (nodelist == null)
                    return null;

                int nodeCount = nodelist.Count;

                // get parameter names
                node = nodelist.Item(0);
                if (node == null)
                    return null;

                XmlAttributeCollection attrCollection = node.Attributes;
                if (attrCollection == null)
                    return null;
                if (attrCollection.Count == 0)
                    return null;

                if (isAddNewColumn)
                {
                    foreach (XmlAttribute attrName in attrCollection)
                    {
                        if (attrName.Name == "IsSelected")
                        {
                            dtData.Columns.Add(attrName.Name, System.Type.GetType("System.Boolean"));
                        }
                        else
                        {
                            dtData.Columns.Add(attrName.Name, System.Type.GetType("System.String"));
                        }
                    }

                    if (!dtData.Columns.Contains("IsSelected"))
                    {
                        dtData.Columns.Add("IsSelected", System.Type.GetType("System.Boolean"));
                    }
                    if (!dtData.Columns.Contains("ServerType"))
                    {
                        dtData.Columns.Add("ServerType", System.Type.GetType("System.String"));
                        // IsServerTypeExists = false;
                    }
                }

                // add rows
                DataRow drAttr = null;
                foreach (XmlNode n in nodelist)
                {
                    drAttr = dtData.NewRow();
                    foreach (XmlAttribute attrVal in n.Attributes)
                    {
                        drAttr[attrVal.Name] = attrVal.Value;
                    }
                    dtData.Rows.Add(drAttr);
                }

                dtData.AcceptChanges();
                
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "GetDataTable");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return dtData;
        }

        private void SaveFilter(FilterType curFilterType)
        {
            try
            {
                DataTable dtFilterData = null;

                switch (curFilterType)
                {
                    case FilterType.ServerType:
                        dtFilterData = dtServerTypes;
                        break;
                    case FilterType.ServerNames:
                        dtFilterData = dtServerNames;
                        break;
                    case FilterType.Fields:
                        dtFilterData = dtFieldNames;
                        break;                    
                }
                if (curFilterType != FilterType.ServerType)
                {
                    for (int rowCount = 0; rowCount < dtFilterData.Rows.Count; rowCount++)
                    {
                        if (currentServerType == dtFilterData.Rows[rowCount]["ServerType"].ToString())
                        {
                            dtFilterData.Rows[rowCount]["IsSelected"] = false;
                        }
                    }

                    for (int iCheckedItems = 0; iCheckedItems < clbFilterXML.CheckedItems.Count; iCheckedItems++)
                    {
                        for (int rowCount = 0; rowCount < dtFilterData.Rows.Count; rowCount++)
                        {
                            if (dtFilterData.Rows[rowCount][0].ToString() == clbFilterXML.CheckedItems[iCheckedItems].ToString())
                            {
                                dtFilterData.Rows[rowCount]["IsSelected"] = true;
                            }
                        }
                    }
                }
                else
                {
                    for (int rowCount = 0; rowCount < dtFilterData.Rows.Count; rowCount++)
                    {
                        if (clbFilterXML.CheckedItems.Contains(dtFilterData.Rows[rowCount][0].ToString()))
                        {
                            dtFilterData.Rows[rowCount]["IsSelected"] = true;
                        }
                        else
                        {
                            dtFilterData.Rows[rowCount]["IsSelected"] = false;
                        }

                    }
                    dtFilterData = dtServerNames;
                    for (int rowCount = 0; rowCount < dtFilterData.Rows.Count; rowCount++)
                    {
                        if (!clbFilterXML.CheckedItems.Contains(dtFilterData.Rows[rowCount]["ServerType"].ToString()))
                        {
                            dtFilterData.Rows[rowCount]["IsSelected"] = false;
                        }
                       

                    }
                    dtFilterData = dtFieldNames;
                    for (int rowCount = 0; rowCount < dtFilterData.Rows.Count; rowCount++)
                    {
                        if (!clbFilterXML.CheckedItems.Contains(dtFilterData.Rows[rowCount]["ServerType"].ToString()))
                        {
                            dtFilterData.Rows[rowCount]["IsSelected"] = false;
                        }


                    }
                }
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "SaveFilter");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void DisplayFilterServerType()
        {
            try
            {
                lblSelectInfo.Text = "Select Server Types";
                clbFilterXML.Items.Clear();

                int serverTypeCount = dtServerTypes.Rows.Count;

                for (int i = 0; i < serverTypeCount; i++)
                {
                    bool IsSelected = (bool)dtServerTypes.Rows[i][1];
                    clbFilterXML.Items.Add(dtServerTypes.Rows[i][0].ToString(), IsSelected);
                }

                currentFilterType = FilterType.ServerType;
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "DisplayFilterServerType");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void DisplayFilterServerIds(string ServerType)
        {
            try
            {
                lblSelectInfo.Text = "Select Servers for Server Type: " + ServerType;
                clbFilterXML.Items.Clear();

                DataRow[] drServerNames = dtServerNames.Select("ServerType='" + ServerType + "'");

                int serverNamesCount = drServerNames.Length; //dtServerNames.Rows.Count;

                for (int i = 0; i < serverNamesCount; i++)
                {
                    bool IsSelected = (bool)drServerNames[i][1];
                    clbFilterXML.Items.Add(drServerNames[i][0].ToString(), IsSelected);
                }

                currentFilterType = FilterType.ServerNames;
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "DisplayFilterServerIds");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }        

        private void DisplayFilterFields(string ServerType)
        {
            try
            {
                lblSelectInfo.Text = "Select Fields for Server Type: " + ServerType;
                clbFilterXML.Items.Clear();

                DataRow[] drFieldNames = dtFieldNames.Select("ServerType='" + ServerType + "'");

                int fieldsCount = drFieldNames.Length;

                for (int i = 0; i < fieldsCount; i++)
                {
                    bool IsSelected = (bool)drFieldNames[i][1];
                    clbFilterXML.Items.Add(drFieldNames[i][0].ToString(), IsSelected);
                }

                currentFilterType = FilterType.Fields;
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "DisplayFilterFields");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void DisplayFilterFieldsAttributes(string ServerType)
        {
            try
            {
                lblSelectInfo.Text = "Select Field Attributes for Server Type: " + ServerType;
                clbFilterXML.Items.Clear();

                DataRow[] drFieldAttributes = dtFieldAttributes.Select("ServerType='" + ServerType + "'");

                int fieldsAttributeCount = drFieldAttributes.Length;

                for (int i = 0; i < fieldsAttributeCount; i++)
                {
                    bool IsSelected = (bool)drFieldAttributes[i][1];
                    clbFilterXML.Items.Add(drFieldAttributes[i][0].ToString(), IsSelected);
                }

                currentFilterType = FilterType.FieldAttributes;
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "DisplayFilterFieldsAttributes");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void setNavigationControl()
        {
            try
            {
                controlWizardFlow = new Hashtable();
                Hashtable flowData;
                int counter = 1;

                flowData = new Hashtable();
                flowData.Add("", FilterType.ServerType);
                controlWizardFlow.Add(counter, flowData);

                for (int typecount = 0; typecount < dtServerTypes.Rows.Count; typecount++)
                {

                    if ((bool)dtServerTypes.Rows[typecount]["IsSelected"])
                    {
                        counter++;
                        flowData = new Hashtable();
                        flowData.Add(dtServerTypes.Rows[typecount][0].ToString(), FilterType.ServerNames);
                        controlWizardFlow.Add(counter, flowData);

                        counter++;
                        flowData = new Hashtable();
                        flowData.Add(dtServerTypes.Rows[typecount][0].ToString(), FilterType.Fields);
                        controlWizardFlow.Add(counter, flowData);
                    }
                }
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "setNavigationControl");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void DisplayCurrentFilter(FilterType curfilterType, string ServerType)
        {

            switch (curfilterType)
            {
                case FilterType.ServerType:
                    {
                        DisplayFilterServerType();
                        break;
                    }
                case FilterType.ServerNames:
                    {
                        DisplayFilterServerIds(ServerType);
                        break;
                    }
                case FilterType.Fields:
                    {
                        DisplayFilterFields(ServerType);
                        break;
                    }
               
            }

        }

        private void controlButtonsStatus()
        {
            try
            {
                if (currentType == controlWizardFlow.Count)
                {

                    btnNext.Enabled = false;
                    btnBack.Enabled = true;

                    this.AcceptButton = this.btnFinish;
                    btnNext.Visible = false;
                    btnFinish.Visible = true;
                    if(clbFilterXML.CheckedItems.Count != 0)
                        btnFinish.Enabled = true;
                    else
                        btnFinish.Enabled = false;
                }
                else if (currentType == 1)
                {
                    btnBack.Enabled = false;

                    btnNext.Enabled = true;
                }
                else
                {
                    //btnNext.Enabled = true;
                    btnBack.Enabled = true;
                }
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "controlButtonsStatus");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #region "Wizard Form Events"

        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {                
                string ServerType = string.Empty;
                SaveFilter(currentFilterType);

                if (currentFilterType == FilterType.ServerType)
                {
                    setNavigationControl();
                }

                if (currentType < controlWizardFlow.Count)
                    currentType++;

                Hashtable checkflowData = (Hashtable)controlWizardFlow[currentType];

                IDictionaryEnumerator enumerator = checkflowData.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    currentServerType = enumerator.Key.ToString();
                    currentFilterType = (FilterType)enumerator.Value;
                }

                
                DisplayCurrentFilter(currentFilterType, currentServerType);

                CheckNextButtonStatus();
                controlButtonsStatus();
                clbFilterXML.Focus();
            }

            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "btnNext_Click");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private bool IsPatchInfoSelected()
        {
            bool flag = false;
            for (int i = 0; i < dtFieldNames.Rows.Count; i++)
            {

                if (dtFieldNames.Rows[i]["fieldname"].ToString() == "PatchInfo")
                {
                    if ((bool)dtFieldNames.Rows[i]["IsSelected"] == true)
                    {
                        flag = true;
                        break;
                    }
                }
            }
            return flag;

        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                string ServerType = string.Empty;

                SaveFilter(currentFilterType);

                if (currentType > 1)
                    currentType--;

                Hashtable checkflowData = (Hashtable)controlWizardFlow[currentType];

                IDictionaryEnumerator enumerator = checkflowData.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    currentServerType = enumerator.Key.ToString();
                    currentFilterType = (FilterType)enumerator.Value;
                }


                DisplayCurrentFilter(currentFilterType, currentServerType);
                CheckNextButtonStatus();
                controlButtonsStatus();
                clbFilterXML.Focus();
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "btnBack_Click");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFilter(currentFilterType);
                DataSet dsFinalFilter = new DataSet("visAd");
                dsFinalFilter.Tables.Add(dtServerTypes.Copy());

                dsFinalFilter.Tables.Add(dtServerNames.Copy());
                dsFinalFilter.Tables.Add(dtFieldNames.Copy());
                dsFinalFilter.Tables.Add(dtFieldAttributes.Copy());

                XMLHelper xmlHelper = new XMLHelper();

                XmlDocument FilterXML = new XmlDocument();

                serverList = xmlHelper.CreateXmlDocument(dsFinalFilter, false);
                visAdHelper.WizardFilterXML = serverList;
                visadHelper.SetDocumentXml(vsoApplication.ActiveDocument, serverList);

                serverList = xmlHelper.CreateXmlDocument(dsFinalFilter, true);

                visAdHelper.FilterXML = serverList;

                visAdHelper.ServerListChanged = false;

                MessageBox.Show(visAd.ServerDataAddIn.Properties.Settings.Default.ServerFilterSet, "Visio Add-In for System Center", MessageBoxButtons.OK, MessageBoxIcon.Information);

                dsFinalFilter.Dispose();
                this.Close();
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "btnFinish_Click");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
               // visAdHelper.FilterXML = serverList;
                this.Close();
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "btnCancel_Click");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
         
            }
            catch (Exception ex)
            {
                throw new vaCustomException(ex, mClassName, "btnCancel_Click");
            }
        }
        private void CheckNextButtonStatus()
        {
            if (clbFilterXML.CheckedItems.Count != 0)
            {
                btnNext.Enabled = true;

                this.AcceptButton = this.btnNext;
                btnNext.Visible = true;
                btnFinish.Visible = false;
                btnFinish.Enabled = false;


            }
            else
            {
                btnNext.Enabled = false;

            }
        }
        #endregion 

        private void clbFilterXML_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (clbFilterXML.CheckedItems.Count != 0)
            {
                btnNext.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
                //btnNext.Visible = false;
                //btnFinish.Visible = true;
                //btnFinish.Enabled = true;

            }
            if (currentType != 1)
            {
                controlButtonsStatus();
            }
        }
        
    
    }
}